package Josephus;

public class Josephus {
	
	
	public static Node create(int n)
	{
		Node head,p;
		head=new Node();
		p=head;
		int i=1;
		Node s = null;
		if(n!=0)
		{
			while(i<=n)
			{
				s=new Node();
				s.data=i++;
				p.next=s;
				p=s;
			}
			p.next=head.next;
		}
		return p.next;	
	}
	
     public static void main(String args[])
     {
    	 int n=41;
    	 int m=3;
    	 Node temp;
    	 Node p=create(n);
    	 n%=m;
    	 while(p!=p.next)
    	 {
    		 for(int i=1;i<n;i++)
    		 {
    			 p=p.next;
    		 }
    		 System.out.print(p.next.data+"->");
    		 temp=p.next;
    		 p.next=temp.next;
    		 p=p.next;
    	 }
    	 System.out.println(p.data);
     }
}
